import db from '../../db.js';
import Router from 'koa-router';
import response from '../../response.js';
import utils from '../../utils/index.js';

const router = new Router();

router.get('/', async (ctx) => {
  const [rows] = await db.query(
    'select userName,id,email,status,create_time as createTime,update_time as updateTime from `user` where isDelete=0',
  );
  ctx.body = response.success({
    data: {
      list: rows,
    },
  });
});
router.delete('/:id', async (ctx) => {
  const { id } = ctx.params || {};
  if (!id && id !== 0) {
    ctx.body = response.fail({ msg: 'id不能为空' });
    return;
  }
  const [hasRows] = await db.query('select id from user where id=? limit 1', [id]);

  if (!hasRows?.length) {
    ctx.body = response.fail({ msg: '数据不存在' });
    return;
  }
  const [result] = await db.query('update   `user` set isDelete=1 where id=?', id);
  if (result?.affectedRows > 0) {
    ctx.body = response.success({
      msg: '删除成功',
    });
  } else {
    ctx.body = response.fail({
      msg: '删除失败',
    });
  }
});
router.put('/', async (ctx) => {
  const { id, userName, email, status } = ctx?.data || {};
  if (id === '' || id === null || id === undefined) {
    ctx.body = response.fail({ msg: 'id不能为空' });
    return;
  }
  const [hasRows] = await db.query('select id from user where id=? limit 1', [id]);
  if (!hasRows?.length) {
    ctx.body = response.fail({ msg: '用户不存在' });
    return;
  }
  const update_time = new Date().getTime();
  const saveData = utils.filterObjectNill({
    userName,
    email,
    update_time,
    status,
  });
  const [result] = await db.query('update user set? where id=?', [saveData, id]);

  if (result?.affectedRows > 0) {
    ctx.body = response.success({ msg: '修改成功' });
  } else {
    ctx.body = response.fail({ msg: '修改失败' });
  }
});
router.post('/', async (ctx) => {
  const { userName, email, status } = ctx.data || {};
  const createTime = new Date().getTime();
  if (!userName && userName !== 0) {
    ctx.body = response.fail({ msg: 'userName not empty' });
    return;
  }
  const [hasRows] = await db.query('select id FROM user where userName=? limit 1', [userName]);
  if (hasRows?.length) {
    ctx.body = response.fail({ msg: 'userName already exists' });
    return;
  }

  if (!email && email !== 0) {
    ctx.body = response.fail({ msg: 'email not empty' });
    return;
  }
  const [hasEmailRows] = await db.query('select id FROM user where email=? limit 1', [email]);
  if (hasEmailRows?.length) {
    ctx.body = response.fail({ msg: 'email already exists' });
    return;
  }

  try {
    const [result] = await db.query(
      'INSERT INTO `user`(`userName`,`email`,`status`,`create_time`,`update_time`) VALUES (?,?,?,?,?)',
      [userName, email, status, createTime, createTime],
    );
    if (result?.affectedRows > 0) {
      ctx.body = response.success({
        code: 0,
        msg: '新增成功',
      });
    } else {
      ctx.body = response.fail({
        code: 0,
        msg: '新增失败',
      });
    }
  } catch (err) {
    ctx.body = response.error({
      msg: err,
    });
  }
});
export default router;
